<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台登录</title>
    <link rel="stylesheet" type="text/css" th:href="@{/plugins/layui/css/layui.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/common.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/login.css}"/>
</head>

<body>

    <div class="layui-container">

        <div class="login-logo">
            <img th:src="@{/images/login/logo.png}">
            <span>SongBoot</span>
        </div>

        <div class="login-form">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">账号: </label>
                    <div class="layui-input-block">
                        <input type="text" id="account" name="account" placeholder="请输入账号" lay-verify="account"
                               autocomplete="off" class="layui-input" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码: </label>
                    <div class="layui-input-block">
                        <input type="text" id="password" name="password" lay-verify="password" placeholder="请输入密码"
                               autocomplete="off" class="layui-input" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="loginSubmit">登录</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <span>或者使用社交账号登入</span>
                        <a href="javascript:;" onclick=""
                            title="QQ登入">
                            <i class="layui-icon layui-icon-login-qq" style="font-size: 30px; padding-left: 10px; color: #1E9FFF;"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>

    <script th:src="@{/plugins/layui/layui.js}" type="text/javascript" charset="utf-8"></script>

    <script>
        layui.use(['form','layer'] , function(){
            var form = layui.form, $ = layui.$, layer = layui.layer;
            //监听提交
            form.on('submit(loginSubmit)', function(formData){
                layer.load(2);
                var param = formData.field;
                $.ajax({
                    url: '/v1/login',
                    data: formData.field,
                    type: 'POST',
                    dataType: 'JSON',
                    success: function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg("登录成功");
                            location.href = '/v1/index';
                        } else {
                            layer.msg(data.msg,{icon:2});
                        }
                    },
                    error: function (error) {
                        layer.closeAll('loading');
                        layer.msg("发生错误");
                    }
                });
                return false;
            });

            // 表单校验
            form.verify({
                account : function(value, item) {
                    if (value === '') {
                        return "账号不能为空";
                    }
                    if (value.length < 3 || value.length > 20) {
                        return "账号长度限制在3至20";
                    }
                },
                password : function (value, item) {
                    if (value === '') {
                        return "密码不能为空";
                    }
                    if (value.length < 5 || value.length > 20) {
                        return "密码长度限制在5至20";
                    }
                }
            });
        });
    </script>
</body>

</html>